﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test.userControl
{


    public partial class TestUseControl : UserControl
    {

        public string Content { get; set; }

        public TestUseControl()
        {
            InitData();
            InitializeComponent();
        }
        private List<TVNode> nodeList = new List<TVNode>();

        private void InitData()
        {
            nodeList.Add(new TVNode() { MenuCode = "cs", MenuId = 1, MenuName = "测试1", ParentId = 0 });
            nodeList.Add(new TVNode() { MenuCode = "cs1", MenuId = 2, MenuName = "测试2", ParentId = 0 });
            nodeList.Add(new TVNode() { MenuCode = "cs2", MenuId = 3, MenuName = "测试3", ParentId = 1 });
            nodeList.Add(new TVNode() { MenuCode = "cs3", MenuId = 4, MenuName = "测试4", ParentId = 2 });
            nodeList.Add(new TVNode() { MenuCode = "cs4", MenuId = 5, MenuName = "测试5", ParentId = 2 });
            nodeList.Add(new TVNode() { MenuCode = "cs5", MenuId = 6, MenuName = "测试6", ParentId = 1 });
            nodeList.Add(new TVNode() { MenuCode = "cs6", MenuId = 7, MenuName = "测试7", ParentId = 1 });
            nodeList.Add(new TVNode() { MenuCode = "cs7", MenuId = 8, MenuName = "测试8", ParentId = 2 });
        }

        private void TestUseControl_Load(object sender, EventArgs e)
        {
            this.lb1.Text = Content;
            this.dgv1.DataSource = nodeList;
            TreeNode rootNode = new TreeNode();
            rootNode.Text = "学员管理系统";
            rootNode.Tag = 0;
            rootNode.ImageIndex = 0;
            tv1.Nodes.Add(rootNode);
            loadNode(rootNode, 0);
            tv1.ExpandAll();
        }

        private void loadNode(TreeNode rootNode, int v)
        {
            TreeNode treeNode = new TreeNode();
            int menuId = 0;
            var child = nodeList.Where(x => x.ParentId == v).ToList();
            if (child.Count>0)
            {
                foreach (var entity in child)
                {
                    treeNode = new TreeNode()
                    {
                        Text = entity.MenuName,
                        Tag = entity.MenuCode,
                        ImageIndex = 1
                    };
                    rootNode.Nodes.Add(treeNode);
                    loadNode(treeNode, entity.MenuId);
                }
            }
            else
            {

            }
            

        }

        private void testToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var ss = this.dgv1.SelectedRows[0];
            MessageBox.Show(ss.Cells["MenuName"].Value.ToString());
        }
    }
}
